import datetime
from xtquant import xtdata
from jili.core.convert import str2datetime
from jili.tool import isnumber
def xttime2datetime(i):
    return datetime.datetime.fromtimestamp(i/1000)
def get_tradeday(count=1):
    dt = xtdata.get_trading_dates(market="SH", count=count)
    tdays = []
    for i in dt:
        tdays.append(xttime2datetime(i))
    return tdays
def get_tradeday_bydate(end,count=1,start=None):
    tdays = []
    if start:
        dt = xtdata.get_trading_dates(market="SH", end_time=str2datetime(end).strftime("%Y%m%d"), start_time=str2datetime(start).strftime("%Y%m%d"))
        for i in dt:
            tdays.append(xttime2datetime(i))
    else:
        dt = xtdata.get_trading_dates(market="SH",end_time=str2datetime(end).strftime("%Y%m%d"),count=count)
        for i in dt:
            tdays.append(xttime2datetime(i))
    return tdays
def istradeday():
    td=get_tradeday()
    now = datetime.datetime.now()
    today = datetime.datetime(now.year, now.month, now.day)
    if today==td[0]:
        return True
    else:
        return False
def obj2xtobj(obj):
    rst = obj
    if "." not in obj:
        i = obj[0]
        if isnumber(i):
            if i>="5":
                rst=rst+".SH"
            elif i<="3":
                rst = rst + ".SZ"
    return rst


